const { rejects } = require("assert");
//promise 内部发生了json解析错误，catch 没有捕捉到
const fs = require("fs");
const { resolve } = require("path");

function asyncFunction() {
    return new Promise((resolve, reject) => {
        fs.readFile("./data.json", "utf8", (err, text) => {
            //读出来的是 Buffer
            console.log("text:");
            console.log(text);
            if (err) {
                console.log(err);
                rejects("spwan some errors when read " + file);
            } else {
                try{
                    console.log("====3333");
                    obj = JSON.parse(text);
                    console.log("====4444");
                    resolve(obj);
                }catch(e){
                    console.log("===5");
                    reject(e);
                }
            }
        })
    });
}

//catch 和 then 的先后顺序有关系吗？
asyncFunction().then(res => {
    console.log("1123");
}).catch(err => {
    console.log("==error==");
    console.log(err);
})

const p = new Promise(function (resolve, reject) {
    setTimeout(function () { 
        try{
            throw new Error('test')
        }catch(e){
            reject(e);
        }
     }, 0)
});
p.catch(err=>{
    console.log("error");
    console.log(err);
})